package com.rpay.web.controller.user;

import com.rpay.common.util.DateUtil;
import com.rpay.common.util.StringUtil;
import com.rpay.common.vo.PcsResult;
import com.rpay.service.config.ServiceThreadLocal;
import com.rpay.service.user.IUserIncomeService;
import com.rpay.web.controller.BaseController;
import com.rpay.web.interceptor.authentic.AccessControl;
import com.rpay.web.interceptor.authentic.AccessControlType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.Date;

/**
 * Title：
 * Description
 *
 * @author fxg06 on 2018/5/21
 * @version 1.0
 */
@RestController
@RequestMapping(value = "api/user/income")
@AccessControl(type = {AccessControlType.SECRET, AccessControlType.ACCESS_TOKEN})
public class UserIncomeController extends BaseController{

    @Autowired
    private IUserIncomeService iUserIncomeService;

    @GetMapping(value = "statistics")
    public PcsResult statistics() {
        return  PcsResult.build(iUserIncomeService.statistics(ServiceThreadLocal.getUserId()));
    }

    @GetMapping(value = "list")
    public PcsResult list(String beginDate, String endDate, Integer pageIndex, Integer pageSize) {
        if (StringUtil.isBlank(beginDate)) {
            beginDate = DateUtil.formatDate(DateUtil.addDays(new Date(), -1));
        }
        if (StringUtil.isBlank(endDate))  {
            endDate = DateUtil.formatDate(DateUtil.addDays(new Date(), -1));
        }
        pageIndex = getPageIndex(pageIndex);
        pageSize = getPageSize(pageSize);
        return PcsResult.build(iUserIncomeService.listUserIncome(getUserId(), beginDate, endDate, pageIndex, pageSize));
    }

    @GetMapping(value = "achievement")
    public PcsResult achievement(@RequestParam @NotNull(message = "用户id不能为空") Integer userId, String beginDate, String endDate, Integer pageIndex, Integer pageSize) {
        if (StringUtil.isBlank(beginDate)) {
            beginDate = DateUtil.formatDate(DateUtil.addDays(new Date(), -1));
        }
        if (StringUtil.isBlank(endDate))  {
            endDate = DateUtil.formatDate(DateUtil.addDays(new Date(), -1));
        }
        pageIndex = getPageIndex(pageIndex);
        pageSize = getPageSize(pageSize);
        return PcsResult.build(iUserIncomeService.listUserIncome(userId, beginDate, endDate, pageIndex, pageSize));
    }
}
